package com.mdd.admin.service.runner;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.admin.service.order.IOrderRefundService;
import com.mdd.admin.service.order.IOrderService;
import com.mdd.admin.service.system.ICouponReceiveService;
import com.mdd.admin.service.system.ICouponService;
import com.mdd.admin.task.*;
import com.mdd.common.entity.system.SystemDevCrontab;
import com.mdd.common.enums.SystemDevCrontabEnum;
import com.mdd.common.mapper.system.SystemDevCrontabMapper;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SysJobRunner implements CommandLineRunner {

    @Resource
    private CronTaskRegistrar cronTaskRegistrar;

    @Resource
    private SystemDevCrontabMapper systemDevCrontabMapper;

    @Resource
    private IOrderService iOrderService;

    @Resource
    private IOrderRefundService iOrderRefundService;

    @Resource
    private ICouponReceiveService iCouponReceiveService;

    @Resource
    private ICouponService couponService;

    @Override
    public void run(String... args) {
        SystemDevCrontab orderCheckCrontab = systemDevCrontabMapper.selectOne(new QueryWrapper<SystemDevCrontab>().eq("status", 1).eq("command", SystemDevCrontabEnum.COMMAND_ORDER_CHECK.getCode()).last("limit 1"));
        SystemDevCrontab refundOrderCheckCrontab = systemDevCrontabMapper.selectOne(new QueryWrapper<SystemDevCrontab>().eq("status", 1).eq("command", SystemDevCrontabEnum.COMMAND_REFUND_ORDER_CHECK.getCode()).last("limit 1"));
        SystemDevCrontab couponReceiveCheckCrontab = systemDevCrontabMapper.selectOne(new QueryWrapper<SystemDevCrontab>().eq("status", 1).eq("command", SystemDevCrontabEnum.COMMAND_COUPON_RECEIVE_CHECK.getCode()).last("limit 1"));
        SystemDevCrontab couponReceiveTimeCheckCrontab = systemDevCrontabMapper.selectOne(new QueryWrapper<SystemDevCrontab>().eq("status", 1).eq("command", SystemDevCrontabEnum.COMMAND_COUPON_RECEIVE_TIME_CHECK.getCode()).last("limit 1"));
        if (orderCheckCrontab != null) {
            OrderCheckRunnable orderCheckTask = new OrderCheckRunnable(orderCheckCrontab.getId(), orderCheckCrontab.getCommand(), iOrderService, systemDevCrontabMapper);
            cronTaskRegistrar.addCronTask(orderCheckTask, orderCheckCrontab.getExpression());
        }
        if (refundOrderCheckCrontab != null) {
            RefundOrderCheckRunnable refundOrderCheckTask = new RefundOrderCheckRunnable(refundOrderCheckCrontab.getId(), refundOrderCheckCrontab.getCommand(), iOrderRefundService, systemDevCrontabMapper);
            cronTaskRegistrar.addCronTask(refundOrderCheckTask, refundOrderCheckCrontab.getExpression());
        }

        if (couponReceiveCheckCrontab != null) {
            CouponReceiveCheckRunnable couponReceiveCheckRunnable = new CouponReceiveCheckRunnable(couponReceiveCheckCrontab.getId(), couponReceiveCheckCrontab.getCommand(), iCouponReceiveService, systemDevCrontabMapper);
            cronTaskRegistrar.addCronTask(couponReceiveCheckRunnable, couponReceiveCheckCrontab.getExpression());
        }

        if (couponReceiveTimeCheckCrontab != null) {
            CouponReceiveTimeCheckRunnable couponReceiveTimeCheckRunnable = new CouponReceiveTimeCheckRunnable(couponReceiveTimeCheckCrontab.getId(), couponReceiveTimeCheckCrontab.getCommand(), couponService, systemDevCrontabMapper);
            cronTaskRegistrar.addCronTask(couponReceiveTimeCheckRunnable, couponReceiveTimeCheckCrontab.getExpression());
        }

    }

}
